import { notEmpty } from '~/utils/common.js'
import AppSettings from '/app_settings/settings.js'
import { setBasicColor } from './basic_color.js'
import { setHttp, setReservedApi, setCustomApi } from './http.js'
import { setDefaultLayoutStyles, setLayoutStyles } from './layout_styles.js'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

export async function reservedSettingsLoadBefore(ComputedSettings) {
  // 导入图标目录下所有svg文件的路径信息，并以KV类型保存svg代码
  // 详见: https://vitejs.dev/guide/features#glob-import
  const icons = import.meta.glob(['/src/reserved/assets/icons/*.svg', '/src/custom/assets/icons/*.svg'])
  for (const path in icons) {
    const name = path.slice(path.lastIndexOf('/') + 1, path.length - 4)
    const svgCode = (await icons[path]()).default
    ComputedSettings.icons[name] = svgCode
  }

  if (notEmpty(AppSettings) && notEmpty(AppSettings.reserved)) {
    const reserved = AppSettings.reserved

    // 系统名称
    if (reserved.name && reserved.name.length > 0) {
      ComputedSettings.name = reserved.name
    }

    // 版权声明
    if (reserved.copyrightText && reserved.copyrightText.length > 0) {
      ComputedSettings.copyrightText = reserved.copyrightText
    }

    // 使用条款和隐私协议
    if (reserved.termsOfUseSettled) {
      ComputedSettings.termsOfUseSettled = reserved.termsOfUseSettled
    }
    if (reserved.privacyPolicySettled) {
      ComputedSettings.privacyPolicySettled = reserved.privacyPolicySettled
    }

    // 门户
    if (notEmpty(reserved.portal)) {
      const portal = reserved.portal
      // 封面图片
      if (portal.coverImageFileName && portal.coverImageFileName.length > 0) {
        ComputedSettings.portal.coverImageFileName = portal.coverImageFileName
      }
      // 封面标题
      if (portal.coverTitle && portal.coverTitle.length > 0) {
        ComputedSettings.portal.coverTitle = portal.coverTitle
      }
      if (portal.coverTitleSize && portal.coverTitleSize.length > 0) {
        ComputedSettings.portal.coverTitleSize = portal.coverTitleSize
      }
      if (portal.coverTitleColor && portal.coverTitleColor.length > 0) {
        ComputedSettings.portal.coverTitleColor = portal.coverTitleColor
      }
      if (portal.coverSubTitle && portal.coverSubTitle.length > 0) {
        ComputedSettings.portal.coverSubTitle = portal.coverSubTitle
      }
      if (portal.coverSubTitleSize && portal.coverSubTitleSize.length > 0) {
        ComputedSettings.portal.coverSubTitleSize = portal.coverSubTitleSize
      }
      if (portal.coverSubTitleColor && portal.coverSubTitleColor.length > 0) {
        ComputedSettings.portal.coverSubTitleColor = portal.coverSubTitleColor
      }
      // 用户名、密码长度
      if (portal.usernameFormat) {
        if (portal.usernameFormat.length > 0) ComputedSettings.portal.minUsernameLen = portal.usernameFormat[0]
        if (portal.usernameFormat.length > 1) ComputedSettings.portal.maxUsernameLen = portal.usernameFormat[1]
        if (portal.usernameFormat.length > 2) ComputedSettings.portal.usernameRegexp = portal.usernameFormat[2]
        if (portal.usernameFormat.length > 3) ComputedSettings.portal.usernameTips = portal.usernameFormat[3]
      }
      if (portal.passwordFormat) {
        if (portal.passwordFormat.length > 0) ComputedSettings.portal.minPasswordLen = portal.passwordFormat[0]
        if (portal.passwordFormat.length > 1) ComputedSettings.portal.maxPasswordLen = portal.passwordFormat[1]
        if (portal.passwordFormat.length > 2) ComputedSettings.portal.passwordRegexp = portal.passwordFormat[2]
        if (portal.passwordFormat.length > 3) ComputedSettings.portal.passwordTips = portal.passwordFormat[3]
      }
    }

    // 管理台框架
    if (notEmpty(reserved.layout)) {
      const layout = reserved.layout
      if (layout.logoImageFileName && layout.logoImageFileName.length > 0) {
        ComputedSettings.layout.logoImageFileName = layout.logoImageFileName
      }
      if (layout.logoTitle && layout.logoTitle.length > 0) {
        ComputedSettings.layout.logoTitle = layout.logoTitle
      }
      if (layout.logoTitleSize && layout.logoTitleSize.length > 0) {
        ComputedSettings.layout.logoTitleSize = layout.logoTitleSize
      }
      if (layout.logoTitleWeight && layout.logoTitleWeight.length > 0) {
        ComputedSettings.layout.logoTitleWeight = layout.logoTitleWeight
      }
      if (layout.logoTitleColor && layout.logoTitleColor.length > 0) {
        ComputedSettings.layout.logoTitleColor = layout.logoTitleColor
      }
    }

    // 网络请求
    if (notEmpty(reserved.http)) {
      setHttp(ComputedSettings, reserved.http)
      if (notEmpty(reserved.http.api)) {
        setReservedApi(ComputedSettings, reserved.http.api)
      }
      if (notEmpty(reserved.http.customApi)) {
        setCustomApi(ComputedSettings, reserved.http.customApi)
      }
    }

    // 控制台日志打印开关
    if (reserved.openLog) {
      ComputedSettings.openLog = reserved.openLog
    }
  }
}

export async function reservedSettingsLoadBeforeMount(ComputedSettings, appInstance) {
  // 全局注册ElementPlus提供的图标
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    appInstance.component(key, component)
  }
}

export async function reservedSettingsLoadAfter(ComputedSettings, appInstance) {
  if (notEmpty(AppSettings) && notEmpty(AppSettings.reserved)) {
    const reserved = AppSettings.reserved

    // 标签页图标
    if (reserved.faviconFileName && reserved.faviconFileName.length > 0) {
      document.getElementById('favicon').href = `/src/custom/assets/favicon/${reserved.faviconFileName}`
    }

    // 标签页标题
    if (reserved.title && reserved.title.length > 0) {
      document.title = reserved.title
    }

    // 基础颜色
    if (notEmpty(reserved.basicColor)) {
      setBasicColor(ComputedSettings, reserved.basicColor)
    }

    // 管理台框架
    setDefaultLayoutStyles(ComputedSettings)
    if (notEmpty(reserved.layout)) {
      setLayoutStyles(ComputedSettings, reserved.layout)
    }
  }
}
